package com.bwie.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.Order;
import com.bwie.pojo.User;
import com.bwie.service.IUserService;
import com.bwie.service.OrderService;
import com.bwie.mapper.OrderMapper;
import com.bwie.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
* @author 陶仕豪
* @description 针对表【tb_order(订单表)】的数据库操作Service实现
* @createDate 2025-04-21 16:08:38
*/
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order>
    implements OrderService{

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    RestTemplate restTemplate;
    @Autowired
    IUserService iUserService;
    @Override
    public R getOrderById(Integer id) {
        //获取订单 信息
        Order order = orderMapper.selectById(id);
        if(order==null){
            return R.ERROR(404,"订单不存在");
        }
        //获取用户信息
//        String  url="http://localhost:9001/user/index/getUserById/"+order.getUserId();
//        R r = restTemplate.postForObject(url, null, R.class);
        R r = iUserService.getUserById(order.getUserId());

        //把map转换成字符串
        String u = JSON.toJSONString(r.getData());
        //再把字符串转换成对象
        User user = JSON.parseObject(u, User.class);
        //返回订单信息
        order.setUser(user);
        return R.OK(order);
    }
}




